package 双指针或滑动窗口.爱生气的书店老板1052;

class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int total = 0;
        int n = customers.length;

        // 步骤1：计算基础满意数
        for (int i = 0; i < n; i++) {
            if (grumpy[i] == 0) {
                total += customers[i];
            }
        }

        // 步骤2：滑动窗口计算最大增益
        int maxIncrease = 0;
        int currentIncrease = 0;

        // 初始化窗口
        for (int i = 0; i < minutes; i++) {
            if (grumpy[i] == 1) {
                currentIncrease += customers[i];
            }
        }
        maxIncrease = currentIncrease;

        // 滑动窗口
        for (int i = minutes; i < n; i++) {
            int left = i - minutes;
            if (grumpy[left] == 1) {
                currentIncrease -= customers[left];
            }
            if (grumpy[i] == 1) {
                currentIncrease += customers[i];
            }
            maxIncrease = Math.max(maxIncrease, currentIncrease);
        }

        return total + maxIncrease;
    }
}
